using Lzfy_His_Service.Models;
using Lzfy_His_Service.Repositories;

namespace Lzfy_His_Service.Services
{
    public class RefundService : IRefundService
    {
        private readonly IRefundMasterRepository _refundMasterRepository;
        private readonly IRefundAttachRepository _refundAttachRepository;
        private readonly IRefundAcctRepository _refundAcctRepository;

        public RefundService(IRefundMasterRepository refundMasterRepository, IRefundAttachRepository refundAttachRepository, IRefundAcctRepository refundAcctRepository)
        {
            _refundMasterRepository = refundMasterRepository;
            _refundAttachRepository = refundAttachRepository;
            _refundAcctRepository = refundAcctRepository;
        }



        public async Task<IEnumerable<RefundResponse>> GetAllRefundsAsync()
        {
            var masters = await _refundMasterRepository.GetAllAsync();
            var responses = new List<RefundResponse>();

            foreach (var master in masters)
            {
                var response = await BuildRefundResponseAsync(master);
                responses.Add(response);
            }

            return responses;
        }











        private async Task<RefundResponse> BuildRefundResponseAsync(RefundMaster master)
        {
            var attachments = await _refundAttachRepository.GetByRelIdAsync(master.RelId!);
            
            var response = new RefundResponse
            {
                Id = master.Id,
                InputDate = master.InputDate,
                PatientId = master.PatientId,
                PatName = master.PatName,
                IdNumber = master.IdNumber,
                PhoneNumber = master.PhoneNumber,
                RefundBankName = master.RefundBankName,
                RefundBankCardNo = master.RefundBankCardNo,
                RefundBankOpenName = master.RefundBankOpenName,
                RefundBankCity = master.RefundBankCity,
                RefundAmount = master.RefundAmount,
                PublicAgentName = master.PublicAgentName,
                PublicAgentPhoneNumber = master.PublicAgentPhoneNumber,
                PublicAgentRelation = master.PublicAgentRelation,
                PublicAgentRelationId = master.PublicAgentRelationId,
                PublicAgentRefundBankName = master.PublicAgentRefundBankName,
                PublicAgentRefundBankCardNo = master.PublicAgentRefundBankCardNo,
                PublicAgentRefundBankOpenName = master.PublicAgentRefundBankOpenName,
                PublicAgentRefundBankCity = master.PublicAgentRefundBankCity,
                AuditUserId = master.AuditUserId,
            AuditUserName = master.AuditUserName,
            AuditResult = master.AuditResult,
                AuditDate = master.AuditDate,
                RefundTypeName = master.RefundTypeName,
                RefundTypeId = master.RefundTypeId,
                AuditComment = master.AuditComment,
                RelId = master.RelId,
                BankCardHolder = master.BankCardHolder,
                RefundBalance = master.RefundBalance,
                RefundBalanceSn = master.RefundBalanceSn,
                AcctId = master.AcctId,
                Attachments = attachments.Select(a => new RefundAttachResponse
                {
                    Id = a.Id,
                    InputDate = a.InputDate,
                    AttachName = a.AttachName,
                    AttachId = a.AttachId,
                    // FilePaths属性在RefundAttachResponse中不存在，需要移除或在模型中添加该属性
                    // 由于 RefundAttachResponse 未定义 FileId 属性，此处移除该字段映射
                    FilePaths = a.FilePaths,
                    FileId = a.FileId,
                    AttachFilePath = null, // 文件路径字段，暂时为空
                    RelId = a.RelId
                }).ToList()
            };

            return response;
        }






    }
}